package solution;

import hex.HexMove;
import hex.HexState;

import java.util.ArrayList;

class Point{
	int x,y;
	static final Point MIDDLE = new Point(5, 5);
	public Point(int x, int y){
		this.x = x; this.y = y;
	}
	
	public Point(String lastMove){
		System.out.println(lastMove);
		x = Integer.parseInt(lastMove.split("-")[0]);
		System.out.println("p2");
		y = Integer.parseInt(lastMove.split("-")[1]);
		System.out.println("p3");
	}
	
	public ArrayList<Point> getSurrounding(){
		ArrayList<Point> result = new ArrayList<Point>();
		if (y+1<6)result.add(new Point(x,y+1));
		if (y-1>=0)result.add(new Point(x,y-1));
		if (x+1<6)result.add(new Point(x+1,y));
		if (x-1>=0)result.add(new Point(x-1,y));
		if (x-1>=0 && y+1<6)result.add(new Point(x-1,y+1));
		if (x+1<6 && y-1>=0)result.add(new Point(x+1,y-1));
		return result;
	}
	public String toString()
	{
		String s = "x: "  + x + " y: " + y;
		return s;
	}
	public boolean equals(Point o){
		return o.x==x && o.y==y;
	}
	public static HexMove toHexMove(Point p){
		return new HexMove(p.x, p.y);
	}
	
	public ArrayList<Point> getPossiblePoints(HexState hs){
		ArrayList<Point> points = new ArrayList<Point>();
		int xNew = x, yNew = y;	 //Go to north position
		xNew--; yNew--;
		points.add(new Point(xNew - 1, yNew));
		points.add(new Point(xNew, yNew -1));
		xNew = x; yNew = y;	 //Go to South west position
		yNew++;
		points.add(new Point(xNew - 1, yNew));
		points.add(new Point(xNew + 1, yNew + 1));
	 	xNew = x; yNew = y;	 //Go to South east position
	 	xNew++;
	 	points.add(new Point(xNew, yNew - 1));
	 	points.add(new Point(xNew + 1, yNew + 1));
	 	points = normalizePossible(points,hs);
	 	return points;
	} 
	
	
	public ArrayList<Point> normalizePossible(ArrayList<Point> points, HexState hs){ 
		for(int i = 0; i < points.size(); i++){
			if((points.get(i).x < 0 || points.get(i).y < 0) && hs.board[points.get(i).x][points.get(i).y] == HexState.emptySym){
				points.remove(i);
				i--;
			}
		}
		return points;
	}
	
	public static ArrayList<Point> getAllPoints(ArrayList<Point> prevMoves, HexState hs){
		ArrayList<Point> allMoves = new ArrayList<Point>();
		ArrayList<Point> result = new ArrayList<Point>();
		for(Point p : prevMoves) allMoves.addAll(p.getPossiblePoints(hs));
		for(Point p: allMoves) if(!result.contains(p) && hs.board[p.x][p.y] == HexState.emptySym) result.add(p);
		return result; 
	}
	
	//@return array list with moves that only have 
	public ArrayList<Point> removeBlockedMoveOptions(ArrayList<Point> prevMoves, ArrayList<Point> possMoves, HexState hs){	
		ArrayList<Point> newMoves = new ArrayList<Point>();
		Point possTemp, hereTemp;
		for(int i = 0; i < possMoves.size(); i++){
			possTemp = possMoves.get(i);
			for(int j = 0; j < prevMoves.size(); j++){
				hereTemp = prevMoves.get(j);
				if(getDist(hereTemp, possTemp) == 2){
					Point[] dubMovesBetween = Rope.getBothPathsBetweenPoints(hereTemp, possTemp);
					boolean pathIsClear = true;
					for(int k = 0; k < dubMovesBetween.length; k++){
						if(!(hs.board[dubMovesBetween[k].x][dubMovesBetween[k].y] == HexState.emptySym || hs.board[dubMovesBetween[k].x][dubMovesBetween[k].y] == HexState.homeSym)){
							pathIsClear = false;
						}
					}
					if(!pathIsClear)
						possMoves.remove(possTemp);
				}
			}
		}
		return newMoves;
	}
	//@return number of hex blocks between two points including that point
	public static int getDist(Point a, Point b){
		int dx = b.x - a.x;
		int dy = b.y - a.y;
		int distance = (Math.abs (dx) + Math.abs (dy) + Math.abs(dx + dy)) / 2;
		return distance;		
	}
	/*
	public int getPointValue(HexState hs,int[][] board, ArrayList<Point> prevMoves, Point p){
		for(Point ourPrevMoves:prevMoves){
			if(new Rope(ourPrevMoves,p).hasTwoMovesBetween(ourPrevMoves,p,board) && getDist(ourPrevMoves,p)==2) return 9;
			
			if(new Rope(ourPrevMoves,p).isClear(hs,ourPrevMoves,p) == new Point(-1,-1) && getDist(ourPrevMoves,p)==4) return 8;
			
			if(new Rope(ourPrevMoves,p).hasOneMoveBetween(ourPrevMoves,p,board) || getDist(ourPrevMoves,p)==2) return 5;
		}
		return 0;		
	}
	*/
}